package com.tstudy.laoshibang.community;

import java.util.List;

import com.tstudy.laoshibang.R;
import com.tstudy.laoshibang.base.BaseApplication;
import com.tstudy.laoshibang.base.CONSTANT;
import com.tstudy.laoshibang.base.HttpManager;
import com.tstudy.laoshibang.mode.CommunityMessage;
import com.tstudy.laoshibang.utils.CommonUtil;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
/**
 * 吐槽消息页adapter
* @CopyRight tstudy.com.cn
* @author wuyahui
* @E-mail wuyahui0124505@163.com
* @version 1.0.0
* @Date 2015年6月3日 上午11:24:26
 */
public class CommunityMessageListAdapter extends BaseAdapter{
	LayoutInflater mInflater;
	private List<CommunityMessage> mItems;
	public CommunityMessageListAdapter() {
		 mInflater = LayoutInflater.from(BaseApplication.mContext);
	}
	
	int imageWidth = CommonUtil.dip2px(70);
	
	@Override
	public int getCount() {
		return mItems == null ? 0:mItems.size();
	}
	
	public void setData(List<CommunityMessage> items) {
		setData(items,true);
	}
	public void setData(List<CommunityMessage> items,boolean isRefresh) {
		mItems = items;
		if (isRefresh) {
			notifyDataSetChanged();
		}
	}
	
	public List<CommunityMessage> getData() {
		return mItems;
	}
	
	@Override
	public CommunityMessage getItem(int position) {
		return mItems == null ? null:mItems.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder viewHolder;
		if(convertView ==  null){
			viewHolder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.community_message_list_item, null);
			viewHolder.userAvatar = (ImageView)convertView.findViewById(R.id.community_message_list_item_user_avatar);
			viewHolder.userName = (TextView)convertView.findViewById(R.id.community_message_list_item_user_name);
			viewHolder.createTime = (TextView)convertView.findViewById(R.id.community_message_list_item_create_time);
			viewHolder.content = (TextView) convertView.findViewById(R.id.community_message_list_item_content);
			viewHolder.communityContent = (TextView) convertView.findViewById(R.id.community_message_list_item_txt);
			viewHolder.communityImg = (ImageView) convertView.findViewById(R.id.community_message_list_item_img);
			viewHolder.splitLine = (View)convertView.findViewById(R.id.community_message_list_split_line);
			convertView.setTag(viewHolder);
		}else{
			viewHolder = (ViewHolder) convertView.getTag();
		}
		if(viewHolder != null){
			setConvertView(viewHolder,position,convertView);
		}
		return convertView;
	}
	public void setConvertView(final ViewHolder viewHolder,final int position,View view){
		final CommunityMessage communityMessage = (CommunityMessage) getItem(position);
		if (communityMessage.user != null) {
			viewHolder.userName.setText(communityMessage.user.nick);
			HttpManager.getInstance().loadEditAvatar(viewHolder.userAvatar,communityMessage.user.imgIdxName+CONSTANT.AVATAR_URL,R.drawable.profile_avatar_default);
		}
		viewHolder.content.setText(communityMessage.comContent);
		viewHolder.createTime.setText(CommonUtil.timelineDateStr(Long.parseLong(communityMessage.createTime)));
		
		if (communityMessage.idxes == null || communityMessage.idxes.size() == 0) {//文字吐槽
			viewHolder.communityContent.setVisibility(View.VISIBLE);
			viewHolder.communityImg.setVisibility(View.GONE);
			if(communityMessage.tcType.getKey() == 1) {
				viewHolder.communityContent.setText(communityMessage.tcTitle);
			}else {
				viewHolder.communityContent.setText(communityMessage.tcContent);
			}
		}else{
			viewHolder.communityContent.setVisibility(View.GONE);
			viewHolder.communityImg.setVisibility(View.VISIBLE);
			HttpManager.getInstance().loadCommonImage(viewHolder.communityImg,communityMessage.idxes.get(0).imgIdxName + CommonUtil.buildCropUrl(imageWidth, imageWidth));
		}
		view.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (mActionCallBack != null) {
					mActionCallBack.onCommunityClick(communityMessage.tcType.getKey(),communityMessage.tcId, position);
				}
			}
		});
		viewHolder.communityImg.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (mActionCallBack != null) {
					mActionCallBack.onCommunityClick(communityMessage.tcType.getKey(),communityMessage.tcId, position);
				}
			}
		});
		viewHolder.userAvatar.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (mActionCallBack != null) {
					mActionCallBack.onAvatorClick(communityMessage.user.userNo);
				}
			}
		});
	}
	
	static class ViewHolder{
		ImageView userAvatar;
		TextView userName;
		TextView content;
		TextView createTime;
		ImageView communityImg;
		TextView communityContent;
		View splitLine;
	}
	static class TopViewHolder{
		TextView title;
	}
	
	public interface AdapterCallBack{
		public void onAvatorClick(String userNo);
		public void onCommunityClick(int type,int communityId,int position);
	}
	AdapterCallBack mActionCallBack;
	public void setAdatperCallBack(AdapterCallBack actionCallBack){
		mActionCallBack = actionCallBack;
	}
}
